<html>
<head>
<#assign basePath=request.contextPath>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>后台管理系统</title>
    <!-- Bootstrap Core CSS -->
    <#include "/WEB-INF/views/template/bootMainCss.ftl" parse=true encoding="utf-8">
    <link rel="stylesheet" href="${basePath}/bootstrap/css/form.lzy.css">
    <link rel="stylesheet" href="${basePath}/bootstrap/css/good.lzy.css">
    <style>
        .form-group{ margin-bottom: 0;}
        .verify-tip{display: none; margin-top: 5px; margin-bottom: 10px; color: #a94442;font-size: 85%;}
        .to{position:relative; width:100px;height:100px;border-radius:50%; overflow: hidden; background-color: #cccccc; margin-left: -10%; display: inline-block;}
        select.form-control{ width: 196px !important;}
    </style>
</head>
<body>
<div style="padding: 0 15px;">
    <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-lzy">
                <div class="panel-body">
                    <h4 class="tips-lzy">提示:</h4>
                    <ul class="tips-list-lzy">
                        后台用户，默认初始密码为：111111，如果该手机号码存在会员用户，会员密码将自动改为：111111，统一密码。
                        若需要设置新密码，请自行修改。
                    </ul>
                </div>
            </div>
            <div class="panel panel-default">
                <div class="panel-body">
                    <div class="row">
                        <div class="col-lg-12">
                            <form id="myForm">
                                <input type="hidden" id="_ctx" value="${basePath}"/>
                                <div class="row">
                                    <div class="col-xs-4 form-group">
                                        <div class="input-group">
                                            <span class="input-group-addon">登录名</span>
                                            <input type="hidden" name="id" id="id" value="${adminUser.id?c}" >
                                            <input type="text" class="form-control" name="loginName" id="loginName" value="${adminUser.loginName!}" maxlength="16">
                                        </div>
                                        <span class="verify-tip">该登录名已存在!</span>
                                    </div>
                                    <div class="col-xs-4 form-group">
                                        <div class="input-group">
                                            <span class="input-group-addon">姓名</span>
                                            <input type="text" class="form-control" name="realName" id="realName" value="${adminUser.realName!}" maxlength="10">
                                        </div>
                                    </div>
                                    <div class="col-xs-4 form-group">
                                        <div class="input-group">
                                            <span class="input-group-addon">&nbsp;&nbsp;性&nbsp;&nbsp;&nbsp;别&nbsp;&nbsp;</span>
                                            <select class="form-control required" id="sex" name="sex">
                                                <#if (adminUser.sex)??>
                                                <option value="0" <#if adminUser.sex == "0">selected</#if>>女</option>
                                                <option value="1" <#if adminUser.sex == "1">selected</#if>>男</option>
                                                <#else>
                                                    <option value="0">女</option>
                                                    <option value="1">男</option>
                                                </#if>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                                <br>
                                <div class="row">
                                    <div class="col-xs-4 form-group">
                                        <div class="input-group">
                                            <span class="input-group-addon">手机号</span>
                                            <input type="text" class="form-control" maxlength="11"
                                                   name="phone" id="phone" value="${adminUser.phone!}">
                                        </div>
                                    </div>
                                    <div class="col-xs-4 form-group">
                                        <div class="input-group">
                                            <span class="input-group-addon">邮箱</span>
                                            <input type="text" class="form-control" name="email" id="email" value="${adminUser.email!}" maxlength="20">
                                        </div>
                                    </div>
                                    <div class="col-xs-4 form-group">
                                        <div class="input-group">
                                            <span class="input-group-addon">所属部门</span>
                                            <select class="form-control" id="deptId" name="deptId">
                                                <option value="">请选择部门</option>
                                            <#if deptList?? >
                                                <#list deptList as dept>
                                                    <option value="${dept.id?c}"
                                                        <#if (adminUser.deptId)??><#if dept.id == adminUser.deptId >selected </#if> </#if>>
                                                    ${dept.deptName!}</option>
                                                </#list>
                                            </#if>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                                <br>
                                <div class="row">
                                    <div class="col-xs-4 form-group">
                                        <div class="input-group">
                                            <span class="input-group-addon">推荐人</span>
                                            <input type="text" class="form-control" maxlength="11"
                                                   name="userPermission" id="userPermission" value="${adminUser.userPermission!}" readonly>
                                            <input type="hidden" name="userId" id="userId" value="">
                                            &nbsp;<button type="button" class="btn btn-primary" onclick="resetPwd();">选择</button>
                                        </div>
                                    </div>
                                </div>
                                <br>
                                <div class="form-group">
                                    <span class="control-label col-sm-2 col-xs-4 detail-span">&nbsp;头像：</span>
                                    <div class="to">
                                        <input class="obj-input" id="avatar" name="avatar" value="${adminUser.avatar!}" type="hidden">
                                    <#if (adminUser.avatar)??>
                                        <img class="obj-img" src="${adminUser.avatar!}" height="100%">
                                        <span class="input-file-edit">
                                        <input type="file" data-action="goods" data-image="avatar"
                                               onchange="uploadImg(this)">
                                        </span>
                                    <#else>
                                        <img class="obj-img" src="" value="" height="100%" style="display: none;">
                                        <span class="input-file">
                                        <input type="file" data-action="goods" data-image="avatar"
                                               onchange="uploadImg(this)">
                                        </span>
                                    </#if>
                                    </div>
                                </div>
                                <br>
                                <div class="row">
                                    <div class="form-group input-group">
                                        <label>&nbsp;&nbsp;&nbsp;后台用户状态&nbsp;&nbsp;&nbsp;</label>
                                        <input type="radio" name="state" value="0"  checked> 正常使用
                                        <input type="radio" name="state" value="2" > 锁定用户
                                    </div>
                                </div>
                                <hr>
                                <div class="row" align="center">
                                    <div class="col-xs-12">
                                        <button type="button" class="btn btn-primary" onclick="formSubmit();">确定</button>
                                        <button type="button" class="btn btn-primary" onclick="closePage();">关闭</button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>


</div>
<#include "/WEB-INF/views/template/bootMainJs.ftl" parse=true encoding="utf-8">
<script>
    var action="${action!}";
    var _parent = parent;
    // 初始化
    $(function () {
        if (action == "update") {
            $('#loginName').attr("readonly","readonly");
        }

        $('#sex').val('${adminUser.sex!}');
        if ($('#sex').val() == null) {
            $('#sex').val("0");
        }
        if ('${adminUser.state!}' == '2') {
            $("input[name=state]:eq(1)").attr("checked",'checked');
        } else {
            $("input[name=state]:eq(0)").attr("checked",'checked');
        }

    });

    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
    //关闭窗口并刷新页面
    function closePage() {
        parent.layer.close(index);
    }
    if (action == "create"){
        $("#loginName").blur(function(){
            checkName();
        });
    }
    function resetPwd(id) {
        layer.open({
            type: 2,
            area: ['900px', '80%'],
            title: '选择推荐人',
            content: ['${basePath}/admin/manager/userPermission', 'yes']
        });


    }
    function checkName(){
        var obj = $('#loginName').val();
        var tip = $(".verify-tip");
        if(obj.length > 3){
            $.ajax({
                type:"GET",
                url:"${basePath}/admin/manager/checkLoginName",
                dataType: 'json',
                data:{
                    loginName:obj
                },
                success:function(data){
                    if(data == false){
                        tip.show();
                    }
                    else{
                        tip.hide();
                    }
                },
                error:function(){
                    console.log("请求失败")
                }
            })
        }
    }
    function formSubmit(){
        var bsVal = $("#myForm").data('bootstrapValidator');
        bsVal.validate();
        if(bsVal.isValid()) {
            console.log($('#deptId').val())
            if($(".verify-tip:visible").length > 0){
                return false
            }
            else{
                $.ajax({
                    type: 'POST', // You can change this to GET if you want to submit via GET
                    url: '${basePath}/admin/manager/' + action,
                    dataType: 'json',
                    data: $("#myForm").serialize(),
                    success: function (data) {
                        console.log(data)
                        if (data.code == '0' || data.code == '0000') {
                            layer.alert('操作成功!', {
                                icon: 6
                            },function(){
                                parent.layer.closeAll();
                            });
                        } else {
                            layer.alert(data.desc, {
                                icon: 5
                            });
                        }
                    },
                    error: function () {
                        layer.alert('系统错误', {
                            icon: 5
                        });
                    }
                })
            }
        }
    }
    $('#myForm').bootstrapValidator({
        fields: {
            loginName: {
                validators: {
                    notEmpty: {
                        message: '请输入登录名'
                    },
                    regexp: {
                        regexp: regName,
                        message: '登录名只能由4到16位的字母、数字或下划线组成'
                    }
                }
            },
            realName: {
                validators: {
                    notEmpty: {
                        message: '请输入真实姓名'
                    },
                    regexp: {
                        regexp: regRreaName,
                        message: '请输入汉字'
                    }
                }
            },
            phone: {
                validators: {
                    notEmpty: {
                        message: '请输入电话号码'
                    },
                    regexp: {
                        regexp: regTel,
                        message: '电话号码格式不正确'
                    }
                }
            },
            email: {
                validators: {
                    regexp: {
                        regexp: regEmail,
                        message: '邮箱格式不正确'
                    }
                }
            }
        }
    })
</script>
</body>
</html>
